package com.example.library.controller;

import com.example.library.entity.Lib;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller

@SessionAttributes("libs")

@RequestMapping("/library")
public class LibController {

    @GetMapping("/list")
    String list(Model model, @SessionAttribute(required = false) List<Lib> libs) {
        if (libs == null) {
            libs = new ArrayList<>();
        }
        model.addAttribute("libs", libs);
        return "list";  //返回名为"list"的视图，即templates中的list.html
    }
    @GetMapping("/add")
    String addForm() {
        return "add"; //返回名为"add"的视图，即templates中的add.html
    }
    @PostMapping("/add")
    String add(Model model, @SessionAttribute(required = false)  List<Lib> libs,
               @RequestParam String picture_url,@RequestParam String name,@RequestParam String author,@RequestParam String chubanshe,@RequestParam String riqi,@RequestParam String number) {
        if (libs == null) {
            libs = new ArrayList<>();
        }
        model.addAttribute("libs", libs);
        Lib lib = new Lib();
        lib.setContent(picture_url,name,author,chubanshe,riqi,number);
        libs.add(lib);
        return "list"; //返回名为"success"的视图，即templates中的success.html
    }
    @GetMapping("/delete")
    String delete(Model model, @SessionAttribute List<Lib> libs,
                  @RequestParam int libId) {
        libs.remove(libId);
        return "list";
    }
    @GetMapping("/edit")
    String editForm(Model model, @SessionAttribute List<Lib> libs,
                    @RequestParam int libId) {
        Lib lib = libs.get(libId);
        model.addAttribute("lib", lib);
        model.addAttribute("libId", libId);
        return "edit";//返回名为"edit"的视图，即templates中的edit.html
    }

    @PostMapping("/edit")
    String edit(Model model, @SessionAttribute List<Lib> libs,
                @RequestParam String picture_url,@RequestParam String name,@RequestParam String author,@RequestParam String chubanshe,@RequestParam String riqi,@RequestParam String number,
                @RequestParam int libId) {
        libs.get(libId).setContent(picture_url,name,author,chubanshe,riqi,number);

        model.addAttribute("libs", libs);
        return "list";
    }
    @RequestMapping("/tologin")
    String tologin() {
        return "login";  //返回名为"list"的视图，即templates中的list.html
    }
    @RequestMapping("user")
    String login(Model model,@RequestParam String username,@RequestParam String password,HttpServletRequest request) {
        HttpSession session =request.getSession();
        if("adminer".equals(username)&&"123".equals(password)){
            System.out.println(username);
            System.out.println(password);
            //model.addAttribute("username",username);
            session.setAttribute("username",username);
            return "list";
        }
        //model.addAttribute("user",user);
        System.out.println(username);
        System.out.println(password);
        System.out.println("not true");
        return "login";  //返回名为"list"的视图，即templates中的list.html
    }



}

